# -*- coding:utf-8 -*-
from apps.models import News, Kind

from flask import Blueprint, request, render_template, session

kind = Blueprint("kind", __name__)


@kind.route('/kind/<int:type_id>/')
def cat(type_id):
    user = session.get('user')
    # 查询所有种类
    kind_list = Kind.query.all()
    # 获取当前页码
    page = request.values.get('page', default=1, type=int)
    # 查询类别ID为type_id的新闻数据
    type = Kind.query.filter_by(type_id=type_id).first()
    news_list = News.query.filter_by(type_id=type.type_id).order_by("-create_time").paginate(page=page, per_page=20)
    hot_news = News.query.filter_by(type_id=type.type_id).order_by("-click_num").limit(10).all()
    return render_template('kind/kind.html', kind_list=kind_list, type=type, news_list=news_list.items,
                           hot_news=hot_news, pagination=news_list, user=user)
